package com.zlsy.learning.leetcode.arraycorrelation.array1431;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author zhouliang
 * @date 2020/6/3 21:08
 */
public class KidsWithCandies {

    /**
     * 自己想的
     */
    public static List<Boolean> kidsWithCandiesByJava8(int[] candies, int extraCandies) {
        //1、找出int[] candies的最大值
        //2、遍历candies里面的所有元素加上extraCandies如果大于等于最大值则为true，否则为false
        if (candies.length == 0) {
            return new ArrayList<>();
        }
        int max = IntStream.of(candies).max().getAsInt();
        Boolean[] arrays = new Boolean[candies.length];
        for (int i = 0; i < candies.length; i++) {
            int candy = candies[i];
            arrays[i] = candy + extraCandies >= max;
        }
        return Stream.of(arrays).collect(Collectors.toList());
    }

    /**
     * 官方解法
     */
    public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int n = candies.length;
        int maxCandies = 0;
        for (int i = 0; i < n; ++i) {
            //找最大值
            maxCandies = Math.max(maxCandies, candies[i]);
        }
        List<Boolean> ret = new ArrayList<Boolean>();
        for (int i = 0; i < n; ++i) {
            ret.add(candies[i] + extraCandies >= maxCandies);
        }
        return ret;
    }

    /**
     * 最简单的解题方法居然是最好的！
     */
    private static List<Boolean> kidsWithCandies1(int[] candies, int extraCandies) {
        List<Boolean> list = new ArrayList<>(candies.length);
        int max = 0;
        for (int candy : candies) {
            if (candy > max) {
                max = candy;
            }
        }
        for (int candy : candies) {
            list.add(candy + extraCandies >= max);
        }
        return list;
    }


    public static void main(String[] args) {
        int[] ints = new int[]{5, 5, 6, 9, 1, 2};
        List<Boolean> booleans = kidsWithCandies1(ints, 4);
        System.out.println(booleans);
    }

}
